package com.example.whereismystuff.views;
import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.ItemDataBaseAdapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * New List Adapter is essentiall a custom cursor adapter for the list of items
 * allowing there to be images and etc.
 * @author Null Pointers
 *
 */
public class NewListAdapter extends CursorAdapter {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public NewListAdapter(Context context, Cursor c) {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context); 
    }

    /**
     * Tells the listadapter which layout to use for each row
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mLayoutInflater.inflate(R.layout.row, parent, false);
        return v;
    }



    /**
     * Sets up the information that each row is supposed to display
     */
    @Override
    public void bindView(View v, Context context, Cursor c) {
        String title = c.getString(c.getColumnIndexOrThrow("ITEMNAME"));
        String found = c.getString(c.getColumnIndexOrThrow("FOUND"));
        String date = c.getString(c.getColumnIndexOrThrow("DATE"));
        String imagePath = c.getString(c.getColumnIndexOrThrow("IMAGE"));
        

        TextView title_text = (TextView) v.findViewById(R.id.title);
        if (title_text != null) {
            title_text.setText(title);
        }

        TextView date_text = (TextView) v.findViewById(R.id.item_date);
        if (date_text != null) {
            date_text.setText(date);
        }

        TextView status_text = (TextView) v.findViewById(R.id.item_status);
        if (status_text != null) {
        	if(found.equals("true")){
        		status_text.setText("FOUND");
        	}
        	else if(found.equals("false")){
        		status_text.setText("LOST");
        	}
        	else{
        	status_text.setText("?");
        	}
           
        }       



        ImageView item_image = (ImageView) v.findViewById(R.id.list_image);
        //item_image.setVisibility(ImageView.INVISIBLE);
        if (imagePath != null && imagePath.length() != 0) {
			item_image.setImageBitmap(decodeBitmap(imagePath, 50, 50));
        }


    }
    
	public static Bitmap decodeBitmap(String id,
			int width, int height) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(id, options);

		// Calculate inSampleSize
		options.inSampleSize = calcSize(options, width, height);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(id, options);
	}

	public static int calcSize(BitmapFactory.Options options, int width, int height) {
		final int tempHeight = options.outHeight;
		final int tempWidth = options.outWidth;
		int size = 2;

		if (tempHeight > height || tempWidth > width) {
			if (tempWidth > tempHeight) {
				size = Math.round((float)tempHeight / (float)height);
			} else {
				size = Math.round((float)tempWidth / (float)width);
			}
		}
		return size;
	}
}